<?php
namespace app\home\controller;
use app\common\controller\Home;	
use think\Cache;
class Vod extends Home{
    public function show(){
		$param = zanpiancms_param_url();
		if($param['dir']){
			$param['id'] = getlist($param['dir'],'list_dir','list_id');
		}else{
			$param['dir'] = getlist($param['id'],'list_id','list_dir');
		}
		$info=$this->Lable_List($param);
		$this->assign('param',$param);
        return $this->fetch(DS.$info['list_skin']);			
    }
    public function type(){
		$param = zanpiancms_param_url();
		if($param['dir']){
			$param['id'] = getlist($param['dir'],'list_dir','list_id');
		}else{
			$param['dir'] = getlist($param['id'],'list_id','list_dir');
		}
	    if(!empty($param['mename']) && empty($param['mcid'])){
		    $param['mcid']=get_mcid($param['id'],$param['mename'],'m_ename','m_cid');	
	    }
	    if(!empty($param['mcid']) && empty($param['mename'])){
            $param['mename']=get_mcid($param['id'],$param['mcid'],'m_cid','m_ename');			
	    }
	    if(!empty($param['earea']) && empty($param['area'])){
		    $param['area']=get_area($param['earea'],'ename','name');	
	    }
	    if(!empty($param['area']) && empty($param['earea'])){
		    $param['earea']=get_area($param['area'],'name','earea');	
	    }		
        //config('params',array('id'=>$param['id'],'dir'=>$param['dir'],'mcid'=>$param['mcid'],'mename'=>$param['mename'],'area'=>$param['area'],'earea'=>$param['earea'],'year'=>$param['year'],'letter'=>$param['letter'],'order'=>$param['order'],'p'=>'[PAGE]'));
		$info=$this->Lable_List($param);
		$this->assign('param',$param);
		if($this->request->isAjax()){
		    return $this->fetch(DS.$info['list_skin_type']."_ajax");
		}else{
		    return $this->fetch(DS.$info['list_skin_type']);
		}			
    }
    public function read(){
		$param = zanpiancms_param_url();
        $array_detail = $this->get_cache_detail($param);
		if($array_detail){
		    $this->assign('param',$param);
			$this->assign('show',$array_detail['show']);
            $this->assign('info',$array_detail['read']);			
		    return $this->fetch(DS.$array_detail['read']['vod_skin_detail']);
		}
		abort(404,'内容不存在');
    }
    public function play(){
		$param = zanpiancms_param_url();
        $array_detail = $this->get_cache_detail($param);
		if($array_detail){
			$array_detail=$this->Lable_Vod_Play($param,$array_detail);
		    $this->assign('param',$param);
			$this->assign('show',$array_detail['show']);
            $this->assign('info',$array_detail['read']);			
		    return $this->fetch(DS.$array_detail['read']['vod_skin_play']);
		}
		abort(404,'内容不存在');
    }
    public function vip(){
		$param = zanpiancms_param_url();
        $array_detail = $this->get_cache_detail($param);
		if($array_detail){
			$array_detail=$this->Lable_Vod_Play($param,$array_detail,1);
		    $this->assign('param',$param);
			$this->assign('show',$array_detail['show']);
            $this->assign('info',$array_detail['read']);
            if(is_login()){
				$userinfo = session('user_auth');
			}			
		    //试看结束提示 action= trysee|ispay|play|pwd
		    if($param['type'] == 'trysee'){
			    return view(DS.'vod_player_trysee');
		    }
		    //微信获取密码
		    if($array_detail['read']['vod_play_vip']==3){
			    $pwdcookie=cookie(md5('play_'.$param['id'].'_pwd'));
			    if($pwdcookie!=md5(ip2long(request()->ip()))){
			        $array_detail['read']['play_status'] = 600;
			    }
		    }
		    //视频是否需要权限
		    elseif($array_detail['read']['vod_play_vip'] || $array_detail['read']['vod_play_pay']){
			    //用户是否登录
		        if(empty($userinfo['user_id'])){
				    $array_detail['read']['play_status'] = 500;//用户未登录
			    }else{
				    $user=model('User')->where('user_status',1)->find($userinfo['user_id']);
				    if(empty($user['user_id'])){
					    $array_detail['read']['play_status'] = 501;//没有找到用户或用户被锁定
				    }else{
					    $array_detail['read']['user_score'] = $user['user_score'];
					    $array_detail['read']['user_deadtime'] = $user['user_viptime'];
				    }
			    }			
			    //VIP包月权限
			    if($array_detail['read']['vod_play_vip']==2){
				    if(time() > $user['user_viptime']){
					    $array_detail['read']['play_status'] = 502;//vip到期 提示
				    }
			    }
			    //单片点播权限
			    if($array_detail['read']['vod_play_pay']){
				    if(!model('score')->count_score_log($user['user_id'],3,1,$array_detail['read']['vod_id'])){
					    $array_detail['read']['play_status'] = 503;//未查询到购买记录
				    }
			    }			
		    }
		    //状态提示
		    $this->assign('info',$array_detail['read']);
		    if($array_detail['read']['play_status']==500 || $array_detail['read']['play_status']==501){ //需要登录输出登录提示
			    $array_detail['read']['play_tips'] = $this->fetch(DS.'vod_play_vip_login');
		    }elseif($array_detail['read']['play_status']==502){ //需要VIP播放输出购买VIP页面
			    $array_detail['read']['play_tips'] = $this->fetch(DS.'vod_play_vip_ispay');
		    }elseif($array_detail['read']['play_status']==503){ //单片点播扣费输出扣费提示
			    $array_detail['read']['play_tips'] = $this->fetch(DS.'vod_play_vip_price');
		    }elseif($array_detail['read']['play_status']==600){ //微信获取密码输出扫码页面
			    $array_detail['read']['play_tips'] = $this->fetch(DS.'vod_play_weixin');
		    }else{
				$array_detail['read']['play_status'] = 200;
			    $array_detail['read']['play_tips'] = '播放正常';
		    }
		    //单片付费点播扣点处理
		    if($param['type'] == 'ispay' && $array_detail['read']['play_status']==503){
			    if($user['score'] < $array_detail['read']['vod_play_pay']){
				    $array_detail['read']['play_status'] = 504;//用户影币不足提示充值
				    $array_detail['read']['play_tips'] = $this->fetch(DS.'vod_play_vip_short');
			    }else{
					$ext=model('score')->user_score($user['user_id'],3,-abs($array_detail['read']['vod_play_pay']), 1, $array_detail['read']['vod_id']);
					if($ext!== false){
				        $array_detail['read']['play_status'] = 200;//扣除用户影币成功就改为可播放状态
				        $array_detail['read']['play_tips'] = '播放正常';
					}else{
						$error=model('score')->getError();
						if($error['code']==-2){
							$array_detail['read']['play_status'] = 602;	
			                $array_detail['read']['play_tips'] = $error['msg'];	
						}						
					}
			    }
		    }
       	    if($param['type'] == 'pwd'){
			    $vodpwd=cache($param['id'].'_'.input('pwd/d',''),'',array('prefix'=>'play_pwd'));
			    if($vodpwd){
			        $array_detail['read']['play_status'] = 200;
			        $array_detail['read']['play_tips'] = '播放正常';
			        cache($param['id'].'_'.input('pwd/d',''),NULL,array('prefix'=>'play_pwd'));	
                    cookie(md5('play_'.$param['id'].'_pwd'), md5(ip2long(request()->ip())),$wechat['wechat_playtime']);		  
			    }else{
			        $array_detail['read']['play_status'] = 600;	
			        $array_detail['read']['play_tips'] = $this->fetch(DS.'vod_play_weixin');
			    }
			
            }			
		    //直接输出或ajax返回
		    if($param['type'] == 'pwd'){
			    $data['status']=$array_detail['read']['play_status'];
			    $data['tips']=$array_detail['read']['play_tips'];
			    return json($data);
		    }
		    elseif($param['type'] == 'ispay'){
			    $data['data']=$array_detail['read']['vod_play_pay'];
			    $data['status']=$array_detail['read']['play_status'];
			    $data['info']=$array_detail['read']['play_tips'];
			    return json($data);
		    }else{
		        $this->assign('param',$param);
			    $this->assign('show',$array_detail['show']);
                $this->assign('info',$array_detail['read']);
			    return $this->fetch(DS.'vod_play_vip');
		    }				
		}
		abort(404,'内容不存在');
    }	
    public function news(){
		$param = zanpiancms_param_url();
		$array_detail = $this->get_cache_detail($param);
		config('params',array('id'=>$array_detail['read']['vod_id'],'pinyin'=>$array_detail['read']['vod_letters'],'cid'=>$array_detail['read']['vod_cid'],'dir'=>$array_detail['show']['list_dir'],'p'=>"[PAGE]"));
		if($array_detail){
		    $this->assign('param',$param);
			$this->assign('show',$array_detail['show']);
            $this->assign('info',$array_detail['read']);			
		    return $this->fetch(DS.$array_detail['read']['vod_skin_detail'].'_news');
		}
		abort(404,'内容不存在');
    }
    public function lines(){
		$param = zanpiancms_param_url();
		$array_detail = $this->get_cache_detail($param);
		if($array_detail){
		    $this->assign('param',$param);
			$this->assign('show',$array_detail['show']);
            $this->assign('info',$array_detail['read']);			
		    return $this->fetch(DS.$array_detail['read']['vod_skin_detail'].'_lines');
		}
		abort(404,'内容不存在');
    }
    public function music(){
		$param = zanpiancms_param_url();
		$array_detail = $this->get_cache_detail($param);
		if($array_detail){
		    $this->assign('param',$param);
			$this->assign('show',$array_detail['show']);
            $this->assign('info',$array_detail['read']);			
		    return $this->fetch(DS.$array_detail['read']['vod_skin_detail'].'_music');
		}
		abort(404,'内容不存在');
    }
    public function filmtime(){
		$param = zanpiancms_param_url();
		$array_detail = $this->get_cache_detail($param);
		if($array_detail){
		    $this->assign('param',$param);
			$this->assign('show',$array_detail['show']);
            $this->assign('info',$array_detail['read']);			
		    return $this->fetch(DS.$array_detail['read']['vod_skin_detail'].'_filmtime');
		}
		abort(404,'内容不存在');
    }		
   //从数据库获取数据
	private function get_cache_detail($param){
		if(empty($param['id']) && empty($param['pinyin'])){ return false;}
		//优先读取缓存数据
		if($GLOBALS['config']['cache']['data_cache_vod']){
			$array_detail = Cache::get('data_cache_vod_'.$param['id'].$param['pinyin']);
			if($array_detail){
				return $array_detail;
			}
		}
		//未中缓存则从数据库读取
		$where = array();
		if(!empty($param['id'])){
		    $where['vod_id'] = $param['id'];
		}
		if(!empty($param['pinyin'])){
		    $where['vod_letters'] = $param['pinyin'];
		}		
		$where['vod_status'] = array('eq',1);
		$data = db('vod')->alias('vod')->join('actor actor','actor.actor_vid =vod.vod_id','LEFT')->join('story story','story.story_vid =vod.vod_id','LEFT')->where($where)->find();
		if(!empty($data)){
			//解析标签
			$array_detail = $this->Lable_Vod_Read($data,1);
			if($GLOBALS['config']['cache']['data_cache_vod']){
				Cache::tag('data_vod')->set('data_cache_vod_'.$param['id'].$param['pinyin'],$array_detail,intval($GLOBALS['config']['cache']['data_cache_vod'])); 
			}
			return $array_detail;
		}
		abort(404,'内容不存在');
	}	

}
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
